package com.azdebugit.java8.future;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Author: itdebug
 * @Email: itdebug.fp@cosmos.com
 * @version:: design_pattern_and_otherutils
 * @description:
 * @create: 2021/11/25 1:15.
 */
public class TestMain {
    @Test
    public void futureTest() {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future1 finished!");
            return "future1 finished!";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("future2 finished!");
            return "future2 finished!";
        });
        CompletableFuture<Void> combindFuture = CompletableFuture.allOf(future1, future2);
        try {
            combindFuture.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("future1: " + future1.isDone() + " future2: " + future2.isDone());
    }

    @Test
    public void datei() throws Exception {
        /**
         *通常指定的时间为我们熟知的格式：“yyyy-MM-dd HH:mm:ss”
         *这种时间格式需要转换成Date类型
         *再使用before()、after()进行比较，返回值类型为Boolean布尔型
         */

        //获取当前时间
        Date d1 = new Date();
        String s1 = "2021-01-01 12:00:00";
        //指定日期转换格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d2 = format.parse(s1);
        //before():指定时间是否早于当前时间
        boolean b1 = d2.before(d1);
        System.out.println(b1);
        //after()指定时间晚于当前时间
        boolean b2 = d2.after(d1);
        System.out.println(b2);
    }

    @Test
    public void getTest() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("e"); //转数字时会出错抛出异常
        list.add("4");

        for (String str : list) {
            try {
                int num = Integer.parseInt(str);  //异常点
                System.out.println(num);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            //finally {do something}
            System.out.println("***");
        }
    }

    @Test
    public void test() {
        List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);

        List<List<Integer>> lists = Lists.partition(numList, 3);
        System.out.println(lists);//[[1, 2, 3], [4, 5, 6], [7, 8]]

    }

    @Test
    public void test11() {
        System.out.println((int)(Math.random()*10 + 5));
        List<String> list = setList();
        List<String> result = list.parallelStream()
                .filter(itm -> !itm.equals("a"))
                .collect(Collectors.toList());

        for (String val : result) {
            System.out.println(val);
        }
    }

    List setList() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        return list;
    }
    @Test
    public static void useCompletableFuture(List<MyTask> tasks) {
        long start = System.nanoTime();
        List<CompletableFuture<Integer>> futures =
                tasks.stream()
                        .map(t -> CompletableFuture.supplyAsync(() -> t.calculate()))
                        .collect(Collectors.toList());

        List<Integer> result =
                futures.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList());
        long duration = (System.nanoTime() - start) / 1_000_000;
        System.out.printf("Processed %d tasks in %d millis\n", tasks.size(), duration);
        System.out.println(result);
    }

}
